﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Medianamik.Core
{
    public class NodeExtendedDataCollection
    {
        public List<NodeExtendedDataProperty> ExtendedProperties { get; set; }

        public NodeExtendedDataCollection()
        {
            ExtendedProperties = new List<NodeExtendedDataProperty>();
        }

        public NodeExtendedDataCollection(IEnumerable<NodeExtendedDataProperty> properties)
        {
            ExtendedProperties = properties.ToList();
        }

        public string Serialize()
        {
            var settings = new XmlWriterSettings
                               {
                                   Indent = false,
                                   Encoding = new UnicodeEncoding(false, false)
                               };

            using (var textWriter = new StringWriter())
            {
                using (var writer = XmlWriter.Create(textWriter, settings)) 
                {
                    writer.WriteStartElement("data");
                    writer.WriteStartElement("properties");
                    foreach(var p in ExtendedProperties)
                    {
                        writer.WriteStartElement("property");
                        switch (p.Relationship)
                        {
                            case NodeTypePropertyRelationship.OneToMany:
                                writer.WriteAttributeString("relationship", "one-to-many");
                                break;
                            case NodeTypePropertyRelationship.ManyToMany:
                                writer.WriteAttributeString("relationship", "many-to-many");
                                break;
                            case NodeTypePropertyRelationship.ManyToOne:
                                writer.WriteAttributeString("relationship", "many-to-one");
                                break;
                        }
                        writer.WriteElementString("name", p.Name);
                        writer.WriteStartElement("value");
                        if (p.Value is string || p.Value == null)
                        {
                            writer.WriteCData((string) p.Value);
                        }
                        else if(p.Value is IEnumerable<Guid>)
                        {
                            writer.WriteCData(string.Join(",", ((IEnumerable<Guid>)p.Value).Select(g => g.ToString()).ToArray()));
                        }
                        else
                        {
                            writer.WriteValue(p.Value);
                        }
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.Flush();
                }

                return textWriter.ToString();
            }
        }
    }
}